//
//  indexViewController.m
//  FHC
//
//  Created by alex on 15/2/10.
//  Copyright (c) 2015年 alex. All rights reserved.
//

#import "indexViewController.h"
#import "WWSideViewController.h"
#import "XXTool.h"
#import "IndexPageTwo.h"
#import "IndexPageNewModel.h"


@interface indexViewController ()<UITabBarControllerDelegate>

@property(nonatomic) Boolean isNeedReflush;

@end

@implementation indexViewController{
    IndexPageNewModel *useData;
    IndexViewCellModel *temp;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    //----------------------------------------------------------------------------
    
    _isNeedReflush=true;
    self.navigationItem.title = NSLocalizedString(@"首页", nil);
    
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(clickLeft)]];
    
    self.delegate=self;
    self.tabBar.selectedImageTintColor = [XXTool colorWithProject];
    
    IndexPageTwo *one = [[IndexPageTwo alloc] initWithNewsType:@"1"]; //展商列表
    IndexPageTwo *two = [[IndexPageTwo alloc] initWithNewsType:@"2"]; //行业新闻
    IndexPageTwo *three = [[IndexPageTwo alloc] initWithNewsType:@"3"];  //同期活动
    self.viewControllers = [NSArray arrayWithObjects:one,two,three, nil];
    
    one.hidesBottomBarWhenPushed = true;
    two.hidesBottomBarWhenPushed = true;
    three.hidesBottomBarWhenPushed = true;
    
    UITabBar *tabBar = self.tabBar;
    //tabBar.alpha = 0.9;
    tabBar.backgroundColor = [UIColor whiteColor];
    tabBar.tintColor = [XXTool colorWithProject];
    tabBar.translucent = NO;
    [tabBar setBarTintColor:[XXTool colorWithBar]];
//    CGRect fr=tabBar.frame;
//    fr.size.height=45;
//    fr.origin.y = self.view.frame.size.height - fr.size.height;
//    tabBar.frame=fr;
    tabBar.barStyle=UIBarStyleBlack;
    
    
    UITabBarItem *tabBarItem1 = [[UITabBarItem alloc]
                                 initWithTitle:NSLocalizedString(@"公告栏", nil)
                                 image:[UIImage imageNamed:@"公告栏点击前"]
                                selectedImage:[[UIImage imageNamed:@"公告栏点击后"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
    tabBarItem1.tag = 0;
    tabBarItem1.image = [[UIImage imageNamed:@"公告栏点击前"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    //tabBarItem1.titlePositionAdjustment = UIOffsetMake(0, -2);
    
    UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"行业新闻", nil)
                                                                        image:[UIImage imageNamed:@"行业新闻点击前"]
                                                                 selectedImage:[[UIImage imageNamed:@"行业新闻点击后"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
    tabBarItem2.tag = 1;
    tabBarItem2.image = [[UIImage imageNamed:@"行业新闻点击前"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //tabBarItem2.titlePositionAdjustment = UIOffsetMake(0, -2);
    
    UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"同期活动", nil)
                                                                    image:[UIImage imageNamed:@"同期活动点击前"]
                                                                selectedImage:[[UIImage imageNamed:@"同期活动点击后"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
    tabBarItem3.tag = 2;
    tabBarItem3.image = [[UIImage imageNamed:@"同期活动点击前"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //tabBarItem3.titlePositionAdjustment = UIOffsetMake(0, -2);

//    tabBarItem1.imageInsets=UIEdgeInsetsMake(-1, 0, 1, 0);
//    tabBarItem2.imageInsets=UIEdgeInsetsMake(-1, 0, 1, 0);
//    tabBarItem3.imageInsets=UIEdgeInsetsMake(-1, 0, 1, 0);
    one.tabBarItem = tabBarItem1;
    two.tabBarItem = tabBarItem2;
    three.tabBarItem = tabBarItem3;
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:
                                                         [XXTool systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor colorWithRed:206/255.0 green:206/255.0 blue:206/255.0 alpha:1.0]}forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[XXTool systemFontOfSize:12],NSForegroundColorAttributeName:[XXTool colorWithProject]} forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    
    
    //--------------------------默认停留在行业新闻
    /*
    self.selectedIndex = 1;
    self.selectedViewController = two;
     */
}

-(void)getData{
    [IndexPageNewModel getMoreIndexNewsWithType:@"3" nowNewsCount:0
                                     completion:^(IndexPageNewModel *data) {
                                         if (data!=nil)
                                         {
                                             if (data.list.count==0)
                                             {
//                                                 [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多新闻", nil)];
                                             }
                                             else
                                             {
                                                 if(_isNeedReflush){
                                                     _isNeedReflush=false;
                                                     useData=data;
                                                     temp = useData.list[0];
                                                     [WebController pushWebControllerWithSourceVC:self requestString:temp.contentabout mapTitle:temp.news_title];
                                                 }
                                                 
                                             }
                                         }
                                         
                                     }];
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
//    self.view.frame = CGRectMake(0
//                                 , 64
//                                 , [UIScreen mainScreen].bounds.size.width
//                                 , [UIScreen mainScreen].bounds.size.height-64);
   // self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

-(void)clickLeft
{
    [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController.sideViewController showLeftView];
    
}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if(viewController == self.viewControllers[2]){
        _isNeedReflush=true;
        [self getData];

        return NO;
    }else{
        return  YES;
    }
    
}

@end
